package com.sun.handler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.spring.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.entity.EnhanceUserExcel;
import com.sun.service.UserExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 自定义校验规则
 */
@Component
public class MyDefinitionVerifyHandler implements IExcelVerifyHandler<EnhanceUserExcel> {

    @Autowired
    private UserExcelService userExcelService;


    /**
     * 校验导入的用户名唯一
     * @param userExcel
     * @return
     */
    @Override
    public ExcelVerifyHandlerResult verifyHandler(EnhanceUserExcel userExcel) {
        //设置默认验证为true
        ExcelVerifyHandlerResult handlerResult = new ExcelVerifyHandlerResult();
        int count = userExcelService.count(new QueryWrapper<EnhanceUserExcel>().eq("user_name", userExcel.getUserName()));
        if(count > 0){
            //存在
            handlerResult.setSuccess(false);
            handlerResult.setMsg("导入的信息中包含已存在的用户，筛选后重新导入！");
            return handlerResult;
        }
        handlerResult.setSuccess(true);
        return handlerResult;
    }
}
